home *** CD-ROM | disk | FTP | other *** search
/ Amiga Desktop Video CD / Amiga DeskTop Video CD.iso / install / toolalias / source / file.c next >
C/C++ Source or Header  |  1994-06-23  |  3KB  |  147 lines

  1. /*
  2.  * File I/O for ToolAlias.
  3.  * Reads (and writes?) configuration files.
  4.  * 
  5.  * mws, 4 February 1993
  6.  */
  7.  
  8. /*
  9.  * structure of .config file (assg. line 0 is first line):
  10.  * 
  11.  *     line 2n:    oldtool
  12.  *     line 2n+1:    newtool
  13.  * 
  14.  * with no leading spaces. MUST be valid format!!!
  15.  * (Will be machine-generated in some future version).
  16.  */
  17.  
  18. #include <dos/dos.h>
  19. #include <proto/dos.h>
  20. #include "list.h"
  21.  
  22. #ifdef TEST
  23. #define Msg(s) printf("error: %s\n", s)
  24. #else
  25. extern void EasyEasyRequest(char *);
  26. #define Msg(s) EasyEasyRequest(s);
  27. #endif
  28.  
  29. /* write a string to a file (in binary format) */
  30. /* '\n' is appended */
  31. void
  32. FWriteString(BPTR fh, char *buf)
  33. {
  34.     FPuts(fh, buf);
  35.     FPutC(fh, '\n');
  36. }
  37.  
  38. /* read a string to a file (in binary format) - returns success */
  39. /* '\n' is stripped and buf null-terminated; assumes dest large enough */
  40. BOOL
  41. FReadString(BPTR fh, char *buf, LONG bufsiz)
  42. {
  43.     UWORD len;
  44.  
  45.     if (FGets(fh, buf, bufsiz))
  46.     {
  47.         len = strlen(buf);
  48.         if (buf[len-1] = '\n')
  49.             buf[len-1] = '\0';    /* '\n' --> '\0' */
  50.         return TRUE;
  51.     }
  52.     return FALSE;
  53. }
  54.  
  55. BOOL
  56. get_config(char *name)
  57. {
  58.     static BOOL firsttime = TRUE;
  59.     char oldname[256], newname[256];
  60.     BPTR fh;
  61.     TOOL *tool;
  62.     BOOL rc = TRUE;
  63.  
  64.     if (fh = Open(name, MODE_OLDFILE))
  65.     {
  66.         free_list();        /* clear old list */
  67.         tool = get_head();
  68.  
  69.         while (FReadString(fh, oldname, 256))
  70.         {
  71.             if (FReadString(fh, newname, 256))
  72.             {
  73.                 if (!((tool = add_tool(tool)) &&
  74.                       set_oldname(tool, oldname) &&
  75.                       set_newname(tool, newname)))
  76.                 {
  77.                     if (tool) rem_tool(tool);
  78.                     Msg("Memory allocation failed");
  79.                     rc = FALSE;
  80.                     break;
  81.                 }
  82.             }
  83.             else    /* invalid file format */
  84.             {    
  85.                 Msg("Invalid config file");
  86.                 rc = FALSE;
  87.                 break;
  88.             }
  89.         }
  90.         Close(fh);
  91.     }
  92.     else if (firsttime)    /* failed first time - no worries */
  93.         firsttime = FALSE;
  94.     else            /* failed on reload - warn user */
  95.         Msg("Couldn't open config file");
  96.  
  97.     if (!rc) free_list();
  98.     return rc;
  99. }
  100.  
  101. /* save current config to named file */
  102. BOOL
  103. write_config(char *name)
  104. {
  105.     TOOL *tool;
  106.     BPTR fh;
  107.  
  108.     if (fh = Open(name, MODE_NEWFILE))
  109.     {
  110.         tool = get_head();
  111.         while (tool)
  112.         {
  113.             FWriteString(fh, tool->oldname);
  114.             FWriteString(fh, tool->newname);
  115.             tool = tool->next;
  116.         }
  117.         Close(fh);
  118.         return TRUE;
  119.     }
  120.     Msg("Couldn't open config file");
  121.     return FALSE;
  122. }
  123.  
  124.  
  125. /******************************************************************************/
  126.  
  127. #ifdef TEST
  128. #include <stdio.h>
  129.  
  130. void
  131. main(int argc, char **argv)
  132. {
  133.     if (get_config("test.config"))
  134.     {
  135.         TOOL *tool = get_head();
  136.         while (tool)
  137.         {
  138.             printf("%s -> %s\n", tool->oldname, tool->newname);
  139.             tool = tool->next;
  140.         }
  141.         free_list();
  142.     }
  143.     else Msg("Couldn't get test.config");
  144.  
  145. } /* main */
  146.  
  147. #endif TEST